package com.sep.team4.recentGrade;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sep.team4.R;
import com.sep.team4.data.Order;

public class RecentGradeActivity extends ListActivity {

	private ProgressDialog m_ProgressDialog = null;
	private ArrayList<Order> m_orders = null;
	private OrderAdapter m_adapter;
	private Runnable viewOrders;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		m_orders = new ArrayList<Order>();
		this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
		setListAdapter(this.m_adapter);

		viewOrders = new Runnable() {
			@Override
			public void run() {
				getOrders();
			}
		};
		Thread thread = new Thread(null, viewOrders, "MagentoBackground");
		thread.start();
		m_ProgressDialog = ProgressDialog.show(RecentGradeActivity.this,
				"Please wait...", "Retrieving data ...", true);
	}

	private Runnable returnRes = new Runnable() {

		@Override
		public void run() {
			if (m_orders != null && m_orders.size() > 0) {
				m_adapter.notifyDataSetChanged();
				for (int i = 0; i < m_orders.size(); i++)
					m_adapter.add(m_orders.get(i));
			}
			m_ProgressDialog.dismiss();
			m_adapter.notifyDataSetChanged();
		}
	};

	private void getOrders() {
		try {
			m_orders = new ArrayList<Order>();
			
			//First row
			Order o1 = new Order();
			o1.setGradeName("BPM Toetsnaam");
			o1.setBottomText("6");
			o1.setDay("1");
			o1.setMonth("JAN");
			o1.setIndicator("red");
			
			//Second row
			Order o2 = new Order();
			o2.setGradeName("SEP toetsnaam");
			o2.setBottomText("9");
			o2.setDay("31");
			o2.setMonth("DEC");
			o2.setIndicator("green");
			m_orders.add(o1);
			m_orders.add(o2);
			m_orders.add(o2);
			m_orders.add(o2);
			m_orders.add(o1);
			// TODO real sleep!
			Thread.sleep(1000);
			Log.i("ARRAY", "" + m_orders.size());
		} catch (Exception e) {
			Log.e("BACKGROUND_PROC", e.getMessage());
		}
		runOnUiThread(returnRes);
	}

	private class OrderAdapter extends ArrayAdapter<Order> {

		private ArrayList<Order> items;

		public OrderAdapter(Context context, int textViewResourceId,
				ArrayList<Order> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row, null);
			}
			Order o = items.get(position);
			if (o != null) {
				TextView tt = (TextView) v.findViewById(R.id.toptext);
				TextView bt = (TextView) v.findViewById(R.id.bottomtext);
				TextView dt = (TextView) v.findViewById(R.id.day);
				TextView mt = (TextView) v.findViewById(R.id.month);
				ImageView indicator = (ImageView) v.findViewById(R.id.icon);
				if (tt != null) {
					tt.setText(o.getGradeName());
					dt.setText(o.getDay());
					mt.setText(o.getMonth());
					if (o.getIndicator().equals("red")) {
						indicator.setBackgroundResource(R.drawable.red);
					} else {
						indicator.setBackgroundResource(R.drawable.green);
					} 

				}
				if (bt != null) {
					bt.setText("Cijfer: " + o.getBottomText());
				}
			}
			return v;
		}
	}
}